package com.fenghuajueli.module_host.fragment

import android.view.View
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.baseswitch.SwitchKeyUtils
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog
import com.fenghuajueli.libbasecoreui.ui.CustomerServiceActivity
import com.fenghuajueli.libbasecoreui.user.UserInfoUtils
import com.fenghuajueli.libbasecoreui.utils.JumpActivityUtils
import com.fenghuajueli.libbasecoreui.viewmodel.BaseViewModelFragment2
import com.fenghuajueli.module_home.model.HomePageModel
import com.fenghuajueli.module_host.databinding.FragmentMineBinding
import com.fenghuajueli.module_route.PermissionCenterModuleRoute
import com.fenghuajueli.module_user.activity.login.LoginActivity
import com.fenghuajueli.module_user.activity.userinfo.UserInfoActivity
import com.fenghuajueli.module_user.ui.helpcenter.HelpCenterActivity.Companion.start

class MineFragment: BaseViewModelFragment2<HomePageModel, FragmentMineBinding>() {

    private var commonTipsDialog: CommonTipsDialog? = null

    override fun createViewModel(): HomePageModel {
        return ViewModelProvider(requireActivity()).get(HomePageModel::class.java)
    }

    override fun createViewBinding(): FragmentMineBinding {
        return FragmentMineBinding.inflate(layoutInflater)
    }


    override fun initView(containerView: View?) {
        //改变状态栏的颜色
//        BarUtils.setStatusBarColor(this, ColorUtils.getColor(com.fenghuajueli.libbasecoreui.R.color.white))
        if(SwitchKeyUtils.getPayStatus()){
            //付费
            binding!!.tvLogin.visibility = View.VISIBLE
        }else{
            //不付费
            binding!!.tvLogin.visibility = View.GONE
        }

        binding.tvLogin.setOnClickListener {
            if (UserInfoUtils.getInstance().isLogin){
                JumpActivityUtils.goNormalActivity(requireContext(), UserInfoActivity::class.java)
            }else{
                JumpActivityUtils.goNormalActivity(requireContext(), LoginActivity::class.java)
            }

        }
        binding.tvYs.setOnClickListener {
            ARouter.getInstance().build(PermissionCenterModuleRoute.PRIVACY_SETTING_ACTIVITY)
                .navigation()
        }
        binding.tvAbout.setOnClickListener {
            JumpActivityUtils.goAboutUsActivity(requireContext())
        }
        binding.tvClean.setOnClickListener {
            showCommTipDialog()
        }
        binding.tvFeedback.setOnClickListener {
            start(requireActivity())
        }
        binding.tvKf.setOnClickListener {
            CustomerServiceActivity.start(requireContext())
        }

    }

    override fun initData() {

    }


    private fun showCommTipDialog() {
        hideCommTipDialog()
        commonTipsDialog = CommonTipsDialog(requireContext(), "你确定要清除缓存吗？")
        commonTipsDialog!!.setCancelListener(View.OnClickListener { hideCommTipDialog() })
        commonTipsDialog!!.setConfirmListener(View.OnClickListener {
            hideCommTipDialog()
            showLoadingDialog()
            binding!!.tvClean.postDelayed({
                hideLoadingDialog()
                ToastUtils.showShort("清理成功")
            }, 1500)
        })
        commonTipsDialog!!.show()
    }

    private fun hideCommTipDialog() {
        if (commonTipsDialog != null) {
            commonTipsDialog!!.dismiss()
            commonTipsDialog = null
        }
    }


    override fun onResume() {
        super.onResume()
        if (UserInfoUtils.getInstance().isLogin){
            binding.tvLogin.text = UserInfoUtils.getInstance().userInfoEntity.userInfo.user_name+"\nID:"+UserInfoUtils.getInstance().userInfoEntity.userInfo.user_id
        }else{
            binding.tvLogin.text = "登录/注册"
        }
    }
}